package org.steven.crm.core.service.product;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.steven.sequence.generater.SnowflakeGenerator;
import com.steven.xdb.dbmanager.DBManager;
import com.steven.xdb.pagination.query.AbstractQueryDataList;
import org.apache.commons.lang3.StringUtils;
import org.steven.crm.core.data.CascadeQueryConfig;
import org.steven.crm.core.data.TableRelation;
import org.steven.crm.core.query.CustomerQueryDataList;
import org.steven.crm.core.query.ProductQueryDataList;
import org.steven.crm.core.service.BaseService;
import org.steven.crm.core.service.system.SystemFileService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author wuzw
 * @Date 2024/6/21 14:38
 * @Version 1.0
 */
public class ProductService extends BaseService {
    private static ProductImageService imageService = new ProductImageService();

    private static SystemFileService systemFileService = new SystemFileService();
    @Override
    protected String getTable() {
        return "product";
    }

    @Override
    protected AbstractQueryDataList getQueryDataList() {
        return new ProductQueryDataList();
    }

    @Override
    protected String getDataSourceName() {
        return null;
    }

    @Override
    public void beforeCreate(Map<String, String> data) throws Exception {
        super.beforeCreate(data);

        String id = SnowflakeGenerator.getIndex();
        data.put("id", id);
        List<String> imageFileIds = JSON.parseObject(data.get("imageFileIds"),
                new TypeReference<List<String>>(){});
        imageService.batchSave(id, imageFileIds);
    }

    @Override
    public void beforeUpdate(String id, Map<String, String> data) throws Exception {
        super.beforeUpdate(id, data);

        if (StringUtils.isNotEmpty(data.get("imageFileIds"))) {
            List<String> imageFileIds = JSON.parseObject(data.get("imageFileIds"), new TypeReference<List<String>>(){});
            imageService.batchSave(id, imageFileIds);
        }
    }

    @Override
    protected List<CascadeQueryConfig> getCascadeQueryConfigs() {
        List<CascadeQueryConfig> cascadeQueryConfigs = new ArrayList<>();

        cascadeQueryConfigs.add(CascadeQueryConfig.builder().build().setSrcColumn("id")
                .setValue("images").setTableRelation(TableRelation.MANY2MANY)
                .setMiddleService(imageService).setMiddleTableSrcColumn("productId")
                .setMiddleTableTargetColumn("imageFileId")
                .setTargetService(systemFileService).setTargetColumn("id")
                .setTargetTableFields(new String[] {"path", "width", "height"}));

        return cascadeQueryConfigs;
    }

    public List<Map<String, String>> listAll() throws Exception {
        Map<String, String> condition = new HashMap<>();
        condition.put("deleted", "0");

        return DBManager.query(getSession(), getTable(), condition);
    }
}
